﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProDivingJudge2salmon14
{
    public partial class NewJudgeForm : Form, INewJudgeForm
    {
        private bool isIHaveBeenHere { get; set; }
        public NewJudgeForm()
        {
            InitializeComponent();

            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            OkButton.Click += new EventHandler(OnOk);
            CancelButton.Click += new EventHandler(OnExit);

            LayoutRbtnMouse.Click += new EventHandler(OnLayoutCheck);
            LayoutRbtnKeyboard.Click += new EventHandler(OnLayoutCheck);

            IPText.Text = "255.255.255.255";

            PortText.PromptChar = ' ';
            PortText.InsertKeyMode = InsertKeyMode.Insert;
            PortText.HidePromptOnLeave = true;

            IDText.PromptChar = ' ';
            IDText.InsertKeyMode = InsertKeyMode.Insert;
            IDText.HidePromptOnLeave = true;

            this.isIHaveBeenHere = false;

            this.VisibleChanged += new EventHandler(VisibleCanged);
        }

        private void VisibleCanged(object sender, EventArgs e)
        {
            if (!this.isIHaveBeenHere || this.isIHaveBeenHere)
            {
                IPText.Hide();
                IPLabel.Hide();

                PortText.Hide();
                PortLabel.Hide();
            }
            else 
            {
                IPText.Show();
                IPLabel.Show();

                PortText.Show();
                PortLabel.Show();
            }
        }

        private void OnLayoutCheck(object sender, EventArgs e) 
        {
            RadioButton rbtn = (RadioButton)sender;

            if (EventSetJudgeLayout != null) 
            {
                EventSetJudgeLayout(rbtn.Text);
            }
        }

        private void OnExit(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void OnOk(object sender, EventArgs e)
        {
            EventSetJudgeID(Int32.Parse(IDText.Text.Trim()));
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.isIHaveBeenHere = true;
            this.Close();
        }

        private bool IsDigitsOnlyAndMaxThree(string str)
        {
            string[] stringElem = str.Split('.');

            foreach (string elem in stringElem)
            {
                if (elem.Length > 3 || elem.Length <= 0)
                {
                    return false;
                }
                foreach (char c in elem)
                {
                    if (c < '0' || c > '9')
                        return false;
                }
            }
            return true;
        }

        public event DelegateSetServerPort EventSetServerPort = null;

        public event DelegateSetServerIP EventSetServerIP = null;

        public event DelegateSetJudgeLayout EventSetJudgeLayout = null;

        public event DelegateSetJudgeID EventSetJudgeID = null;
    }
}
